import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver, VantResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver(), VantResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver(), VantResolver()],
    }),
  ],
  // 设置基础路径，与 router 保持一致
  base: '/hzshtgl/dist/',
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    },
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    // 确保资源文件正确生成
    rollupOptions: {
      output: {
        // 确保静态资源文件名稳定
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
      },
    },
  },
  server: {
    port: 3002,
    open: true,
    // 配置代理，处理传统URL格式
    proxy: {
      // 可以在这里配置API代理
    },
    // 确保单页应用的路由正确工作
    historyApiFallback: {
      rewrites: [
        // 将传统格式的URL重写为Vue路由格式
        { from: /^\/hzshtgl\/.*/, to: '/hzshtgl/index.html' },
      ],
    },
  },
})
